package com.fr.report;

import com.fr.base.Style;
import com.fr.base.core.GraphHelper;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.PaintUtils;
import com.fr.report.core.cal.LayerPageReport;
import com.fr.report.core.cal.LayerReport;
import com.fr.report.core.cal.SE;
import java.awt.FontMetrics;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

public class WorkSheet extends AbstractTemplateReport
{
  private static final long serialVersionUID = -8922304295595224713L;
  private WorkSheetAttr workSheetAttr = null;
  private LayerReportAttr layerReportAttr = null;

  public void reset()
  {
    removeAllCellElements();
    removeAllFloatElements();
    this.columnWidthList_DEC.reset();
    this.rowHeightList_DEC.reset();
  }

  public WorkSheetAttr getWorkSheetAttr()
  {
    return this.workSheetAttr;
  }

  public void setWorkSheetAttr(WorkSheetAttr paramWorkSheetAttr)
  {
    this.workSheetAttr = paramWorkSheetAttr;
  }

  public LayerReportAttr getLayerReportAttr()
  {
    return this.layerReportAttr;
  }

  public void setLayerReportAttr(LayerReportAttr paramLayerReportAttr)
  {
    this.layerReportAttr = paramLayerReportAttr;
  }

  public void adjustColumnPreferredWidth(int paramInt)
  {
    Iterator localIterator = cellIterator();
    while (true)
    {
      Style localStyle;
      Object localObject;
      do
      {
        CellElement localCellElement;
        do
        {
          if (!(localIterator.hasNext()))
            return;
          localCellElement = (CellElement)localIterator.next();
        }
        while (localCellElement.getColumn() != paramInt);
        localStyle = localCellElement.getStyle();
        localObject = localCellElement.getValue();
      }
      while (!(localObject instanceof String));
      FontMetrics localFontMetrics = GraphHelper.getFontMetrics(localStyle.getFRFont());
      this.columnWidthList_DEC.set(paramInt, Math.max(this.columnWidthList_DEC.get(paramInt), localFontMetrics.stringWidth((String)localObject)));
    }
  }

  public void adjustRowPreferredHeight(int paramInt)
  {
    Iterator localIterator = cellIterator();
    while (true)
    {
      int j;
      int k;
      do
      {
        CellElement localCellElement;
        do
        {
          if (!(localIterator.hasNext()))
            return;
          localCellElement = (CellElement)localIterator.next();
        }
        while (localCellElement.getRow() != paramInt);
        int i = localCellElement.getColumn();
        j = localCellElement.getRow();
        k = PaintUtils.analyzeCellElementPreferredHeight(localCellElement, this.columnWidthList_DEC.getRangeValue(i, i + localCellElement.getColumnSpan()));
      }
      while (k <= getRowHeight(j));
      setRowHeight(paramInt, k);
    }
  }

  public ResultReport execute(Map paramMap)
  {
    return _execute(paramMap, 0);
  }

  public ResultReport execute4Write(Map paramMap)
  {
    return _execute(paramMap, 1);
  }

  public ResultReport execute4Form(Map paramMap)
  {
    return _execute(paramMap, 2);
  }

  private ResultReport _execute(Map paramMap, int paramInt)
  {
    WorkSheet localWorkSheet;
    try
    {
      localWorkSheet = (WorkSheet)clone();
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      throw new RuntimeException(localCloneNotSupportedException);
    }
    if (paramMap == null)
      paramMap = Collections.EMPTY_MAP;
    if ((paramInt == 0) && (this.layerReportAttr != null))
    {
      if (this.layerReportAttr.isPageQuery())
        return new LayerPageReport(localWorkSheet, paramMap, this.layerReportAttr.getCountPerPage());
      return new LayerReport(localWorkSheet, paramMap);
    }
    SE localSE = new SE(localWorkSheet, paramMap);
    return localSE.execute(paramInt);
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if (("WorkSheetAttr".equals(str1)) || ("LinearReportAttr".equals(str1)) || ("ComplexSheetAttr".equals(str1)))
      {
        if (this.workSheetAttr == null)
          this.workSheetAttr = new WorkSheetAttr();
        paramXMLableReader.readXMLObject(this.workSheetAttr);
      }
      else if ("LayerAttr".equals(str1))
      {
        if (Boolean.valueOf(paramXMLableReader.getElementValue()).booleanValue())
          setLayerReportAttr(new LayerReportAttr());
      }
      else if ("LayerReportAttr".equals(str1))
      {
        if (getLayerReportAttr() == null)
          setLayerReportAttr(new LayerReportAttr());
        paramXMLableReader.readXMLObject(getLayerReportAttr());
      }
      else
      {
        String str2;
        ReportPageAttr localReportPageAttr;
        if (str1.equals("SheetRegion"))
        {
          localReportPageAttr = getReportPageAttr();
          if (localReportPageAttr == null)
          {
            localReportPageAttr = new ReportPageAttr();
            setReportPageAttr(localReportPageAttr);
          }
          if ((str2 = paramXMLableReader.getAttr("prhf")) != null)
            localReportPageAttr.setRepeatHeaderRowFrom(parseIntWithoutException(str2));
          if ((str2 = paramXMLableReader.getAttr("drf")) != null)
            localReportPageAttr.setRepeatHeaderRowTo(parseIntWithoutException(str2));
          if ((str2 = paramXMLableReader.getAttr("prff")) != null)
            localReportPageAttr.setRepeatFooterRowFrom(parseIntWithoutException(str2));
          if ((str2 = paramXMLableReader.getAttr("frff")) != null)
            localReportPageAttr.setRepeatFooterRowTo(parseIntWithoutException(str2));
          if ((str2 = paramXMLableReader.getAttr("pcht")) != null)
          {
            localReportPageAttr.setRepeatHeaderColumnFrom(0);
            localReportPageAttr.setRepeatHeaderColumnTo(parseIntWithoutException(str2));
          }
          if ((str2 = paramXMLableReader.getAttr("pcff")) != null)
            localReportPageAttr.setRepeatFooterColumnFrom(parseIntWithoutException(str2));
        }
        else if (str1.equals("SpreadSheetAttributes"))
        {
          localReportPageAttr = getReportPageAttr();
          if (localReportPageAttr == null)
          {
            localReportPageAttr = new ReportPageAttr();
            setReportPageAttr(localReportPageAttr);
          }
          if ((str2 = paramXMLableReader.getAttr("startRepeatRow")) != null)
            localReportPageAttr.setRepeatHeaderRowFrom(parseIntWithoutException(str2));
          if ((str2 = paramXMLableReader.getAttr("endRepeatRow")) != null)
            localReportPageAttr.setRepeatHeaderRowTo(parseIntWithoutException(str2));
          if ((str2 = paramXMLableReader.getAttr("startRepeatColumn")) != null)
            localReportPageAttr.setRepeatHeaderColumnFrom(parseIntWithoutException(str2));
          if ((str2 = paramXMLableReader.getAttr("endRepeatColumn")) != null)
            localReportPageAttr.setRepeatHeaderColumnTo(parseIntWithoutException(str2));
        }
      }
    }
  }

  private int parseIntWithoutException(String paramString)
  {
    try
    {
      return Integer.parseInt(paramString);
    }
    catch (NumberFormatException localNumberFormatException)
    {
    }
    return -1;
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    if (this.workSheetAttr != null)
      this.workSheetAttr.writeXML(paramXMLPrintWriter);
    if (this.layerReportAttr != null)
      this.layerReportAttr.writeXML(paramXMLPrintWriter);
  }

  public WorkSheet cloneWithoutCell()
    throws CloneNotSupportedException
  {
    WorkSheet localWorkSheet = (WorkSheet)super.cloneWithoutCellCase();
    if ((this.workSheetAttr != null) && (this.workSheetAttr.getDirection() != 2))
    {
      localWorkSheet.workSheetAttr = ((WorkSheetAttr)this.workSheetAttr.clone());
      localWorkSheet.reportPageAttr = null;
    }
    if (this.layerReportAttr != null)
      localWorkSheet.layerReportAttr = ((LayerReportAttr)this.layerReportAttr.clone());
    return localWorkSheet;
  }

  public void convertWithoutCellCaseToResultReport(RWorkSheet paramRWorkSheet)
    throws CloneNotSupportedException
  {
    super.cloneWithoutCellCase4Report(paramRWorkSheet);
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    WorkSheet localWorkSheet = (WorkSheet)super.clone();
    if (this.workSheetAttr != null)
      localWorkSheet.workSheetAttr = ((WorkSheetAttr)this.workSheetAttr.clone());
    if (this.layerReportAttr != null)
      localWorkSheet.layerReportAttr = ((LayerReportAttr)this.layerReportAttr.clone());
    return localWorkSheet;
  }

  public Object deriveClearReportWithReadOnlyAttr()
    throws CloneNotSupportedException
  {
    return super.deriveClearReportWithReadOnlyAttr();
  }

  protected CellElement createDefaultCellElementCase()
  {
    return new DefaultCellElement();
  }
}